import 'package:flutter/material.dart';
import '../models/ddl_item.dart';

class DDLCard extends StatelessWidget {
  final DDLItem ddl;
  final ValueChanged<String> onStatusChanged;

  const DDLCard({super.key, required this.ddl, required this.onStatusChanged});

  String get formattedTime {
    final deadline = ddl.deadline ?? DateTime.now();
    return "${deadline.year}年${deadline.month.toString().padLeft(2, '0')}月"
           "${deadline.day.toString().padLeft(2, '0')}日 "
           "${deadline.hour.toString().padLeft(2, '0')}:"
           "${deadline.minute.toString().padLeft(2, '0')}";
  }

  String get timeLeft {
    final deadline = ddl.deadline ?? DateTime.now();
    final now = DateTime.now();
    final diff = deadline.difference(now);
    if (diff.isNegative) {
      return "已截止";
    }
    final days = diff.inDays;
    final hours = diff.inHours % 24;
    final minutes = diff.inMinutes % 60;
    return "还剩$days天$hours时$minutes分";
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.all(8),
      child: Padding(
        padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
            Row(
              children: [
                Expanded(
                  child: Text(
                    ddl.title,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                if (ddl.isUrgent)
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                      color: Colors.red[100],
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: const Text(
                      '紧急',
                  style: TextStyle(
                        color: Colors.red,
                        fontSize: 12,
                      ),
                    ),
                  ),
              ],
            ),
            const SizedBox(height: 8),
            Text(
              '截止时间: $formattedTime',
              style: TextStyle(color: Colors.grey[600]),
            ),
            const SizedBox(height: 4),
            Text(
              timeLeft,
              style: TextStyle(
                color: ddl.deadline?.difference(DateTime.now()).isNegative ?? false
                    ? Colors.red
                    : Colors.green,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
